package com.example.demo.zaqizabatest.多线程相关;

/**
 * @ClassName 打印
 * @Author lys4134
 * @Date 2021/08/27 14:29:22
 * @Description 打印 创建两个线程，其中一个输出1-52，另外一个输出A-Z。输出格式要求：12A 34B 56C 78D ...
 * @Version 1.0
 **/
@SuppressWarnings("AlibabaAvoidManuallyCreateThread")
public class 打印 {

    private static Object object = new Object();

    private static void printNum() throws InterruptedException {
        synchronized (object) {
            for (int i = 1; i < 53; i++) {
                System.out.print(i+",");
                object.notifyAll();
                if (i % 2 == 0) {
                    if (i != 52){
                        object.wait();
                    }
                }
            }
        }
    }

    private static void printCha() throws InterruptedException {
        synchronized (object) {
            for (char i = 'A'; i <= 'Z'; i++) {
                System.out.print(((char) i) + "    ");
                object.notifyAll();
                if (i != 'Z'){
                    object.wait();
                } else {
                    System.out.println();
                }
            }
        }
    }


    public static void main(String[] args) {

//        Object object = new Object();
//        new Thread(new Number(object)).start();
//        new Thread(new Character(object)).start();

        new Thread(() -> {
            try {
                printNum();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                printCha();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    static class Number implements Runnable {
        private Object object;

        public Number(Object object) {
            this.object = object;
        }

        @Override
        public void run() {
            synchronized (object) {
                for (int i = 1; i < 53; i++) {
                    if (i > 1 && i % 2 == 1) {
                        System.out.print(" ");
                    }
                    System.out.print(i);
                    if (i % 2 == 0) {
                        object.notifyAll();
                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    static class Character implements Runnable {
        private Object object;

        public Character(Object object) {
            this.object = object;
        }

        @Override
        public void run() {
            synchronized (object) {
                for (char i = 'A'; i <= 'Z'; i++) {
                    System.out.print(i);
                    object.notifyAll();
                    if (i < 'Z') {
                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

